package dev.ideas.funkyj.group;

import dev.ideas.funkyj.tuple.Single;
import org.junit.Assert;
import org.junit.Test;

import static dev.ideas.funkyj.group.GroupBy.*;
import static dev.ideas.funkyj.sequence.Sequence.*;
import static org.junit.Assert.*;

/**
 */
public class ToFlat {

    @Test public void t1() {
        final Group<Boolean, Single<Integer>> group1 = from(range(1, 6)).groupBy(even).to(sum);
        final Integer evens = group1.select(true).get();
        final Integer odds = group1.select(false).get();

        assertEquals(evens, (Integer) 12);
        assertEquals(odds, (Integer) 9);
    }

    @Test public void t2() {
        final Group<Integer, Group<Boolean, Single<Integer>>> group1 = from(range(10, 99)).groupBy(_2, even.of(_1)).to(sum);

        final Integer evens20 = group1.select(2).select(true).get();
        assertEquals(evens20, (Integer) 120);
    }

    @Test public void t3() {
        final Group<Integer, Group<Integer, Group<Boolean, Single<Integer>>>> group1 =
                from(range(100, 999)).groupBy(_3, _2, even.of(_1)).to(sum);

        final Integer evens250 = group1.select(2).select(5).select(true).get();
        Assert.assertEquals(evens250, from(range(250, 259)).where(even).to(sum).get());
    }
}
